import { deleteFileByIdApi } from "@/api/common";
import { createVNode } from "vue";
import { message, Modal } from "ant-design-vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { dialogOpts, formState, passData, downloadFileByUrl, init, drawerOpts, formItemColumnMap, formItemColumn } = state;

  const modifyWaybillHandle = (row: Base.GeneraDataType) => {
    dialogOpts.title = `修改运单(${row.waybillNum})`;
    dialogOpts.type = 2;
    dialogOpts.passData = row;
    dialogOpts.component = "ModifyWaybill";
    dialogOpts.visible = true;
  };
  const modifyTimeHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "修改期望取货时间";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyTime";
    dialogOpts.visible = true;
  };
  const modifyWarehouseHandle = () => {
    dialogOpts.width = "820px";
    dialogOpts.title = "修改送货仓库";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyWarehouse";
    dialogOpts.visible = true;
  };
  const modifyPickupAddressHandle = () => {
    dialogOpts.width = "820px";
    dialogOpts.title = "修改取件地址";
    dialogOpts.type = 2;
    dialogOpts.passData = formState.value;
    dialogOpts.component = "ModifyPickupAddress";
    dialogOpts.visible = true;
  };
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 过滤处理 交税方式
  const fltDeclarationType = (row: Array<{ code: string; name: string }>) => {
    if (!row || !row.length) return "-";
    const result = row.map((t: { code: string; name: string }) => t.name);
    return result.join(",");
  };
  const deleteFileHandle = (file: ClientWaybillManagement.FileListRow) => {
    Modal.confirm({
      title: "是否删除附件？",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        return deleteFileByIdApi({ id: file.id, fileUrl: file.fileUrl }).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("删除成功!");
            init(passData.value.id);
          }
        });
      }
    });
  };
  const downloadFileHandle = (file: ClientWaybillManagement.FileListRow) => {
    downloadFileByUrl(file.fileUrl, file.filename);
  };
  const uploadFileHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "附件上传";
    dialogOpts.type = 1;
    dialogOpts.passData = passData.value;
    dialogOpts.component = "UploadFile";
    dialogOpts.visible = true;
  };
  const showOrRetract = () => {
    if (formItemColumnMap.value.length > 3) {
      formItemColumnMap.value = formItemColumnMap.value.slice(0, 3);
    } else {
      formItemColumnMap.value = formItemColumn;
    }
  };

  // 查看转单号
  const SetTransferNumberHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = `${formState.value["waybillNum"]}-转单号`;
    drawerOpts.passData = formState.value;
    drawerOpts.component = "SetNumber";
    drawerOpts.visible = true;
  };

  return {
    modifyWaybillHandle,
    modifyTimeHandle,
    modifyWarehouseHandle,
    modifyPickupAddressHandle,
    fltDeclarationType,
    deleteFileHandle,
    downloadFileHandle,
    uploadFileHandle,
    showInvoiceHandle,
    showOrRetract,
    SetTransferNumberHandle
  };
}
